package org.itheima.googleplay.ui.fragment;

import java.util.List;

import org.itheima.googleplay.R;
import org.itheima.googleplay.bean.AppInfoBean;
import org.itheima.googleplay.http.AppProtocol;
import org.itheima.googleplay.ui.adapter.AppListAdapter;
import org.itheima.googleplay.ui.adapter.SuperBaseAdapter;
import org.itheima.googleplay.ui.fragment.HomeFragment.HomeAdapter;
import org.itheima.googleplay.ui.fragment.LoadingPager.LoadedResult;
import org.itheima.googleplay.ui.holder.AppItemHolder;
import org.itheima.googleplay.ui.holder.BaseHolder;
import org.itheima.googleplay.ui.weiget.BaseListView;
import org.itheima.googleplay.utils.UIUtils;

import android.graphics.Color;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * @包名: org.itheima.googleplay.ui.fragment
 * @类名: AppFragment
 * @作者: 肖琦
 * @创建时间 : 2015-3-22 下午2:40:56
 * 
 * @描述: 应用的界面
 * 
 * @SVN版本: $Rev: 19 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-23 20:03:41 +0800 (星期一, 23 三月 2015) $
 * 
 * @更新描述:
 */
public class AppFragment extends BaseFragment
{
	private AppProtocol			mProtocol;
	private List<AppInfoBean>	mListDatas; // listView的数据

	@Override
	protected View onSuccessView()
	{
		BaseListView mListView = new BaseListView(UIUtils.getContext());

		// adapter ---> list
		mListView.setAdapter(new AppAdapter(mListView, mListDatas));

		return mListView;
	}

	@Override
	protected LoadedResult onLoadData()
	{
		mProtocol = new AppProtocol();

		try
		{
			// 去网络加载数据
			mListDatas = mProtocol.loadData(0);

			return checkState(mListDatas);
		}
		catch (Exception e)
		{
			e.printStackTrace();

			return LoadedResult.ERROR;
		}
	}

	class AppAdapter extends AppListAdapter
	{

		public AppAdapter(AbsListView listView, List<AppInfoBean> datas) {
			super(listView, datas);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected List<AppInfoBean> onLoadMoreData() throws Exception
		{
			return loadMoreData(mDatas.size());
		}
	}

	private List<AppInfoBean> loadMoreData(int index) throws Exception
	{
		return mProtocol.loadData(index);
	}
}
